//=============================================================================
#include "CTexture.h"

//=============================================================================
CTexture::CTexture() {
	ImageName = 0;

    TextureID = 0;

    Width   = 0;
    Height  = 0;
}

//=============================================================================
bool CTexture::OnLoad(std::string File) {
	OnCleanup();

	ilGenImages(1, &ImageName);
	ilBindImage(ImageName);

	if(ilLoadImage(const_cast<char *>(File.c_str())) == false) {
		return false;
	}

	Width 	= ilGetInteger(IL_IMAGE_WIDTH);
	Height 	= ilGetInteger(IL_IMAGE_HEIGHT);

	glGenTextures(1, &TextureID);
	glBindTexture(GL_TEXTURE_2D, TextureID);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //GL_LINEAR);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	//glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());

	gluBuild2DMipmaps(GL_TEXTURE_2D, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());

    return true;
}

//-----------------------------------------------------------------------------
bool CTexture::OnLoad(SDL_Surface* Surface) {
    OnCleanup();

    if(Surface == NULL) {
        return false;
    }

    glGenTextures(1, &TextureID);

    glBindTexture(GL_TEXTURE_2D, TextureID);

    int Mode = GL_RGB;

    if(Surface->format->BytesPerPixel == 4) {
        Mode = GL_RGBA;
    }

    glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    Width   = Surface->w;
    Height  = Surface->h;

    return true;
}

//-----------------------------------------------------------------------------
void CTexture::OnCleanup() {
    if(TextureID > 0) {
        glDeleteTextures(1, &TextureID);
        TextureID = 0;
    }

	if(ImageName > 0) {
    	ilDeleteImage(ImageName);
	}
}

//=============================================================================
void CTexture::Empty(int Width, int Height) {
	OnCleanup();

	unsigned int* Data = (unsigned int*)new GLuint[((Width * Height) * 4 * sizeof(unsigned int))];

	//TextureID = ilutGLLoadImage(const_cast<char *>(File.c_str()));

	this->Width 	= Width;
	this->Height 	= Height;

	glGenTextures(1, &TextureID);
	glBindTexture(GL_TEXTURE_2D, TextureID);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, 4, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, Data);

	delete [] Data;
}

//=============================================================================
void CTexture::Bind() {
    glBindTexture(GL_TEXTURE_2D, TextureID);
}

//=============================================================================
void CTexture::RenderQuad(int X, int Y) {
    RenderQuad(X, Y, Width, Height);
}

//------------------------------------------------------------------------------
void CTexture::RenderQuad(int X, int Y, int Width, int Height) {
    Bind();

    //This combination is matched with the current Ortho view
    //Don't change the Ortho view, otherwise textures will be flipped
    //or rotated
    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
        glTexCoord2f(1, 0); glVertex3f(X + Width, Y, 0);
        glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
        glTexCoord2f(0, 1); glVertex3f(X, Y + Height, 0);
    glEnd();
}

//------------------------------------------------------------------------------
void CTexture::RenderQuad(int X, int Y, int Width, int Height, int SrcX, int SrcY, int SrcWidth, int SrcHeight) {
    Bind();

    float tX = (SrcX / (float)this->Width);
    float tY = (SrcY / (float)this->Height);

    float tX2 = ((SrcX + SrcWidth) / (float)this->Width);
    float tY2 = ((SrcY + SrcHeight) / (float)this->Height);

    //This combination is matched with the current Ortho view
    //Don't change the Ortho view, otherwise textures will be flipped
    //or rotated
    glBegin(GL_QUADS);
        glTexCoord2f(tX, 	tY); 	glVertex3f(X, Y, 0);
        glTexCoord2f(tX2,  	tY); 	glVertex3f(X + Width, Y, 0);
        glTexCoord2f(tX2,  	tY2);  	glVertex3f(X + Width, Y + Height, 0);
        glTexCoord2f(tX, 	tY2);  	glVertex3f(X, Y + Height, 0);
    glEnd();
}

//=============================================================================
